//
// Created by Semir on 2020/5/15.
//

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

bool IsPopOrder(vector<int> pushV, vector<int> popV) {
    if (pushV.empty()) {
        return true;
    }
    stack<int> S;
    int i = 0;
    int j = 0;
    while (j < popV.size()) {
        if (i < pushV.size() && j < popV.size() && pushV[i] == popV[j]) {
            i++;
            j++;
        } else if (!S.empty() && S.top() == popV[j]) {
            S.pop();
            j++;
        } else {
            if (i < pushV.size()) {
                S.push(pushV[i++]);
            } else {
                return false;
            }
        }
    }
    return true;
}

int main() {
    vector<int> pushV = {1,2,3,4,5};
    vector<int> popV = {3,4,2,1,5};
    bool res = IsPopOrder(pushV, popV);
    return 0;

}